from mcp.server.fastmcp import FastMCP

# Create an MCP server
mcp = FastMCP("Math Server")

@mcp.tool()
def add(a: float, b: float) -> float:
    """Add two numbers"""
    return a + b

@mcp.tool()
def subtract(a: float, b: float) -> float:
    """Subtract two numbers"""
    return a - b

@mcp.tool()
def multiply(a: float, b: float) -> float:
    """Multiply two numbers"""
    return a * b

@mcp.tool()
def divide(a: float, b: float) -> float:
    """Divide two numbers"""
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

if __name__ == "__main__":
    mcp.run(transport="stdio")
from typing import List, Dict, Optional, Union
import math
import json

# 基础数学函数实现
def add(a: float, b: float) -> float:
    return a + b

def subtract(a: float, b: float) -> float:
    return a - b

@mcp.tool()
def multiply(a: float, b: float) -> float:
    """乘法运算"""
    return a * b

@mcp.tool()
def divide(a: float, b: float) -> float:
    """除法运算"""
    if b == 0:
        raise ValueError("除数不能为零")
    return a / b

@mcp.tool()
def calculate(expression: str) -> str:
    """计算表达式"""
    try:
        safe_dict = {
            "__builtins__": {},
            "sqrt": math.sqrt,
            "pi": math.pi,
            "e": math.e
        }
        result = eval(expression, safe_dict)
        return f"表达式: {expression}\n结果: {result}"
    except ZeroDivisionError:
        return "错误: 除零错误"
    except Exception as e:
        return f"计算失败: {str(e)}"

if __name__ == "__main__":
    mcp.run(
        transport=config["server"]["transport"],
        port=config["server"]["port"]
    )